package com.darker.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.darker.reggie.common.BaseContext;
import com.darker.reggie.common.DateUtil;
import com.darker.reggie.common.R;
import com.darker.reggie.dto.OrdersDto;
import com.darker.reggie.entity.OrderDetail;
import com.darker.reggie.entity.Orders;
import com.darker.reggie.service.OrderDetailService;
import com.darker.reggie.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    OrdersService ordersService;

    @Autowired
    OrderDetailService orderDetailService;
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        ordersService.submit(orders);
        return R.success("提交成功");
    }

    @GetMapping("/page")
    public R<Page<Orders>> page(int page, int pageSize,String number,String beginTime,String endTime){

        Page<Orders> ordersPage=new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(number),Orders::getNumber,number)
                .gt(StringUtils.isNotEmpty(beginTime),Orders::getOrderTime,beginTime)
                .lt(StringUtils.isNotEmpty(endTime),Orders::getOrderTime,endTime)
                .orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage,queryWrapper);

        return R.success(ordersPage);

    }
    //获取订单信息
    @GetMapping("/userPage")
    public R<Page<OrdersDto>>userPage(int page, int pageSize){
        Page<Orders> ordersPage = new Page<>(page, pageSize);
        Page<OrdersDto> ordersDtoPage = new Page<>();
        //查询订单表的基本信息
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, BaseContext.getCurrentId())
                .orderByDesc(Orders::getOrderTime);
        ordersService.page(ordersPage, queryWrapper);
        //复制ordersPage的数据到ordersDtoPage除了records属性
        BeanUtils.copyProperties(ordersPage,ordersDtoPage,"records");
        List<OrderDetail> orderDetails;
        List<Orders> orderslist = ordersPage.getRecords();
        List<OrdersDto> ordersDtoList=new ArrayList<>();
        OrdersDto ordersDto;
        LambdaQueryWrapper<OrderDetail> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        //处理ordersDto的Page  records属性
        for(Orders orders:orderslist){
            ordersDto=  new OrdersDto();
            BeanUtils.copyProperties(orders,ordersDto);
            //根据订单号查询订单详细数据集合
            lambdaQueryWrapper.eq(OrderDetail::getOrderId,orders.getId());
            orderDetails = orderDetailService.list(lambdaQueryWrapper);
            //清楚eq条件
            lambdaQueryWrapper.clear();

            ordersDto.setOrderDetails(orderDetails);
            ordersDtoList.add(ordersDto);
        }
        ordersDtoPage.setRecords(ordersDtoList);
        return R.success(ordersDtoPage);
    }

}
